<?php
    class Usuario
    {        
        private $db;
        private $id;
        private $login;
        private $senha;
        private $amigo;
        private $userDataArray;
        
        public function __construct()
        {
            $this->db = Zend_Registry::get('db');
        }
        
        public function getUserDataArray()
        {
            return $this->userDataArray;
        }
        
        public function cadastrar($parameters)
        {
            
            $name = $parameters['name'];
            $lastName = $parameters['lastName'];
            $eMail = $parameters['eMail'];
            $password = $parameters['password'];
            
            $sql = "INSERT INTO usuario (eMail,name,lastName,passwd,createData,lastAccess)
                        VALUES ('$eMail','$name','$lastName','$password',NOW(),NOW());";
            
           try
           {
               $this->db->query($sql);
           }
           catch(exception $e)
           {
                echo 'O nome ja existe';
                return FALSE;
           }
           
           return TRUE;
        }
        
        
        public function login($parameters)
        {
            /*
             * Os parametros sao os seguintes:
             * $parameters['email'] = email
             * $parameters['password'] = senha
             * 
             */
            $email = $parameters['email'];
            $passwd = $parameters['password'];
            
            $sql = "SELECT *
                        FROM usuario
                            WHERE eMail = '$email'
                            AND passwd = '$passwd'
                            AND removedData IS NULL;";
            
           $return = $this->db->query($sql)->fetchAll();
           
            if(isset($return[0]))
            {                
                $this->userDataArray = $return;
                $this->updateAccess($email);
                return true;
            }
            
            return FALSE;  
        }
        
        private function updateAccess($eMail)
        {
            $sql = "UPDATE usuario SET
                        lastAccess = 'NOW()'
                            WHERE eMail = '$eMail';";
            $this->db->query($sql);
        }
     }
?>
